/**
 * 初始化展会管理详情对话框
 */
var UserDetail = {
	IMap:{},
    userId:null,
    elem:null
};

/**
 * 关闭此对话框
 */
UserDetail.close = function() {
    parent.layer.close(window.parent.MgrUser.layerIndex);
};

/*
 * 切换Switchery开关函数
 * * switchElement Switchery对象
 * * checkedBool 选中的状态
 */
UserDetail.setSwitchery=function(switchElement, checkedBool) {
    if ((checkedBool && !switchElement.isChecked()) || (!checkedBool && switchElement.isChecked())) {
        switchElement.setPosition(true);
    }
};
/**
 * 初始化Switchery开关
 */
UserDetail.initSwitchery=function(){
	var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
	elems.forEach(function(html) {
		UserDetail.IMap[html.id]= new Switchery(html,{ color: '#038de5', jackColor: '#038de5' });
	});
};

UserDetail.callSetSwitchery = function(){
    UserDetail.setSwitchery(UserDetail.IMap[UserDetail.elem.id],!UserDetail.elem.checked);
}

/**
 * ios7 切换事件
 */
UserDetail.switchChange=function(elem,userId){
    UserDetail.elem = elem;
    UserDetail.userId = userId;
    if(elem.checked){
        Feng.confirm('是否解除冻结?',UserDetail.unfreeze,UserDetail.callSetSwitchery);
    }else{
        Feng.confirm('是否冻结?',UserDetail.freezeAccount,UserDetail.callSetSwitchery);
    }
}

/**
 * 冻结用户账户
 * @param userId
 */
UserDetail.freezeAccount = function () {
    var ajax = new $ax(Feng.ctxPath + "/mgr/freeze", function (data) {
        Feng.success("冻结成功!");
		window.self.parent.MgrUser.table.refresh();
    }, function (data) {
        UserDetail.callSetSwitchery();
        Feng.error("冻结失败!" + data.responseJSON.message + "!");
    });
    ajax.set("userId", UserDetail.userId);
    ajax.start();

};

/**
 * 解除冻结用户账户
 * @param userId
 */
UserDetail.unfreeze = function () {
    var ajax = new $ax(Feng.ctxPath + "/mgr/unfreeze", function (data) {
        Feng.success("解除冻结成功!");
        window.self.parent.MgrUser.table.refresh();
    }, function (data) {
        UserDetail.callSetSwitchery();
        Feng.error("解除冻结失败!");
    });
    ajax.set("userId", UserDetail.userId);
    ajax.start();
}

/**
 * 点击修改按钮时
 * @param userId 管理员id
 */
UserDetail.openChangeUser = function (userId) {
    window.self.parent.MgrUser.openChangeUser(userId);
};

$(function() {
	//初始化Switchery开关
	UserDetail.initSwitchery();
});
